package wxsdkPakage;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.qingning.qn.R;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class WXJumpPlugin implements MethodChannel.MethodCallHandler {
    public static final String CHANNEL = "com.ahj/wxJumpPlugin";
    static MethodChannel channe;
    private Activity activity;
    String appId;

    public WXJumpPlugin(Activity activity) {
        appId = activity.getString(R.string.wxAppid);
        this.activity = activity;
    }

    public static void registerWith(FlutterEngine flutterEngine, Activity activity12) {
        channe = new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL);
        WXJumpPlugin plugin = new WXJumpPlugin(activity12);
        channe.setMethodCallHandler(plugin);
    }


    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        try {
            if (methodCall.method.equals("jumpSmallProgram")) {
                String path = methodCall.argument("url");
                String[] wxData = path.split("\\|");

                IWXAPI api = WXAPIFactory.createWXAPI(activity, appId);

                WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
                req.userName = wxData[1]; // 填小程序原始id
                req.path = wxData[2];                  ////拉起小程序页面的可带参路径，不填默认拉起小程序首页，对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"。
                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版，体验版和正式版
//                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;// 体验版
                api.sendReq(req);


            } else if (methodCall.method.equals("liveNumber")) {
            }


        } catch (Exception e) {
            e.printStackTrace();
            result.success("");
        }
    }
}
